﻿using HostAssistantCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HostAssistant
{
    public partial class adddb : U1Form
    {
        public adddb()
        {
            InitializeComponent();
            lblhid.Visible = false;
        }

        #region 添加mysql
        private void simpleButton1_Click(object sender, EventArgs e)
        {

            try
            {
                if (String.IsNullOrWhiteSpace(txtuser.Text))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("数据库名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (String.IsNullOrWhiteSpace(txtpwd.Text))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("数据库密码不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                Regex r = new Regex(@"[a-zA-Z](?=.*?[1-9].*?)[a-zA-Z1-9]+|[1-9](?=.*?[a-zA-Z].*?)[a-zA-Z1-9]+");
                if (!r.IsMatch(txtpwd.Text.Trim()))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("数据库密码格式错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                Regex r2 = new Regex(@"((?=[\x21-\x7e]+)[^A-Za-z0-9])");
                if (!r2.IsMatch(txtpwd.Text.Trim()))
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("数据库密码格式错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (txtpwd.Text.Trim().Length < 8)
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show("数据库密码格式错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }


                OpHelper.CallWithTimeout(Addmssql, 5000);

            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                if (lblhid.Text == "1")
                {
                    lblhid.Text = "0";
                    Form1 f1 = (Form1)this.Owner;
                    f1.op("数据库列表");
                    this.Dispose();
                }

            }






        
        }

        void Addmssql()
        {

          
            splashScreenManager1.ShowWaitForm();
            splashScreenManager1.SetWaitFormCaption("请等待");
            splashScreenManager1.SetWaitFormDescription("加载中...");
         
                string password = "";
                string port = "";
                var set = Data<HostAssistantCore.setting>.GetInstance().GetModelList("select *  from setting", null);
                var mysql = set.FirstOrDefault(o => o.type == "mysql");
                if (mysql == null)
                {
                    splashScreenManager1.CloseWaitForm();
                    DevExpress.XtraEditors.XtraMessageBox.Show("请先到“系统配置”进行相关参数配置", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;

                }
                string ip = mysql?.ip ?? "";
                port = mysql?.port ?? "";
                string user = mysql?.username ?? "";
                password = mysql?.userpwd ?? "";
                SqlInfo mysqlinfo = new SqlInfo();
                mysqlinfo.Mdbname = this.txtuser.Text.Trim().ToString();//数据库名
                mysqlinfo.Muser = this.txtuser.Text.Trim().ToString();//用户名
                mysqlinfo.Mpass = this.txtpwd.Text.Trim().ToString();//密码
                mysqlinfo.Mtime = DateTime.Now.ToString();//时间
                mysqlinfo.Mpath = mysql.MyPath + "\\" + mysqlinfo.Mdbname;




                MySqlDbHelper mdb = new MySqlDbHelper();
                mdb.Initialize(ip, "mysql", user, password, port);
                if (mdb.ConnectionTest() != null)
                {
                    splashScreenManager1.CloseWaitForm();
                    DevExpress.XtraEditors.XtraMessageBox.Show("未能连接上数据库,请检查数据库账号密码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DataSet ds_mysql_dbname = mdb.GetDataSet(CommandType.Text, "show databases", null);
                if (ds_mysql_dbname.Tables[0].Rows.Count != 0)
                {

                    DataTable dt_dbnames = ds_mysql_dbname.Tables[0];

                    foreach (DataRow dr in dt_dbnames.Rows)
                    {
                        if (dr["database"].ToString() == mysqlinfo.Mdbname)
                        {

                            splashScreenManager1.CloseWaitForm();
                            DevExpress.XtraEditors.XtraMessageBox.Show("数据库已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);


                            return;
                        }
                    }
                }
                if (!mdb.Update("create database " + mysqlinfo.Mdbname))
                {
                    splashScreenManager1.CloseWaitForm();
                    DevExpress.XtraEditors.XtraMessageBox.Show("添加数据库'" + mysqlinfo.Mdbname + "'失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                string mdb_add_user = "grant all privileges on " + mysqlinfo.Mdbname + ".* to '" + mysqlinfo.Mdbname + "'@'localhost' identified by '" + mysqlinfo.Mpass + "' with grant option";
                string mdb_remote_user = "grant all privileges on " + mysqlinfo.Mdbname + ".* to '" + mysqlinfo.Mdbname + "'@'%' identified by '" + mysqlinfo.Mpass + "' with grant option";
                if (mdb.Touch(mdb_add_user))//添加用户
                {
                    mdb.Touch("flush privileges");
                    mysqlinfo.type = "mysql";
                    string query = "INSERT INTO SqlInfo(Mpath,  type,Mdbname,Muser,Mpass,Mtime)VALUES( @Mpath, @type,@Mdbname,@Muser,@Mpass,@Mtime)   ";
                    Data<SqlInfo>.GetInstance().Add(query, mysqlinfo);
                    splashScreenManager1.CloseWaitForm();
                    DevExpress.XtraEditors.XtraMessageBox.Show("成功添加数据库", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    mdb.Update("drop database " + mysqlinfo.Mdbname);
                    splashScreenManager1.CloseWaitForm();
                    DevExpress.XtraEditors.XtraMessageBox.Show("添加数据库用户失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }


            lblhid.Text = "1";
        }
            #endregion

            #region 返回
            private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

    }
}
